package com.flying.cms.api;

import java.io.File;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.IOUtils;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.helper.SequenceHelper;
import com.flying.common.util.Base64;
import com.flying.common.util.Codes;
import com.flying.common.util.DateUtils;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.data.Data;
import com.flying.framework.service.AbstractService;

@Service("api.attachment")
public class Attachment  extends AbstractService{
	private final static DateFormat df = new SimpleDateFormat("MMdd");

	@MethodInfo("上传文件并返回文件地址")
	@DaoUpdate(entity="user_base_info")
	public Data uploadFile(
			@Param(value="file_data", required=true, desc="文件二进制流的Base64编码数据") String file_data,
			@Param(value="user_id", required=true, desc="用户ID") int user_id
		
		) throws Exception {
		final Date now = new Date();
		final byte[] fileData = Base64.decode(file_data);
		final String uploadPath = module.getModuleConfig().getConfig("uploadPath");
		final String uploadURL = module.getModuleConfig().getConfig("uploadURL");
		
		final String file_name = user_id + "." + SequenceHelper.nextVal(module, "upload") + ".png";
		final File filePath = new File(uploadPath + "/" + DateUtils.getYear(now) + "/" + df.format(now));
		if(!filePath.exists()) filePath.mkdirs();
		
		FileOutputStream fos = new FileOutputStream(uploadPath + "/" + DateUtils.getYear(now) + "/" + df.format(now) +"/" + file_name);
		IOUtils.write(fileData, fos);
		fos.close();
		
		final String file_url = uploadURL + "/" + DateUtils.getYear(now) + "/" + df.format(now) +"/" + file_name;
		return new Data(Codes.CODE, Codes.SUCCESS, "url", file_url);
	}
	
	@MethodInfo("创建新对象")
	@DaoCreate(entity="t_attachment")
	public Data create(
		
			@Param(value="content_id", required=false, desc="") int content_id,
			@Param(value="name", required=false, desc="") String name,
			@Param(value="content_type", required=false, desc="") String content_type,
			@Param(value="size", required=false, desc="") int size,
			@Param(value="path", required=false, desc="") String path,
			@Param(value="tag", required=false, desc="") String tag,
			@Param(value="desc", required=false, desc="") String desc,
			@Param(value="del_flag", required=false, desc="") int del_flag,
			@Param(value="create_date", required=false, desc="") Date create_date
		
		) throws Exception {
		
		return new Data("del_flag",0,"create_date", new Date());
	}

	@MethodInfo("修改对象")
	@DaoUpdate(entity="t_attachment")
	public Data update(
		
			@Param(value="attachment_id", required=true, desc="") int attachment_id,
			@Param(value="content_id", required=false, desc="") int content_id,
			@Param(value="name", required=false, desc="") String name,
			@Param(value="content_type", required=false, desc="") String content_type,
			@Param(value="size", required=false, desc="") int size,
			@Param(value="path", required=false, desc="") String path,
			@Param(value="tag", required=false, desc="") String tag,
			@Param(value="desc", required=false, desc="") String desc,
			@Param(value="del_flag", required=false, desc="") int del_flag,
			@Param(value="create_date", required=false, desc="") Date create_date,
			@Param(value="update_date", required=false, desc="") Date update_date
		
		) throws Exception {
		
		return null;
	}

	@MethodInfo("删除对象")
	@DaoRemove(entity="t_attachment")
	public Data remove(
		
			@Param(value="attachment_id", required=true, desc="") int attachment_id
		
		) throws Exception {
		
		return null;
	}

	@MethodInfo("查询所有对象")
	@DaoQuery(entity="t_attachment")
	public Data findAll() throws Exception {
		return null;
	}

	@MethodInfo("根据主键查询")
	@DaoQuery(entity="t_attachment", single=true)
	public Data findByPrimaryKey(
		
			@Param(value="attachment_id", required=true, desc="") int attachment_id
		
		) throws Exception {
		return null;
	}

}